home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / test / fullscreen.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1999-01-07  |  6.2 KB  |  245 lines

  1. //
  2. // "$Id: fullscreen.cxx,v 1.4 1999/01/07 19:17:54 mike Exp $"
  3. //
  4. // Fullscreen test program for the Fast Light Tool Kit (FLTK).
  5. //
  6. // This demo shows how to do many of the window manipulations that
  7. // are popular on SGI programs, even though X does not really like
  8. // them.  You can toggle the border on/off, change the visual to
  9. // switch between single/double buffer, and make the window take
  10. // over the screen.
  11. //
  12. // Normally the program makes a single window with a child GL window.
  13. // This simulates a program where the 3D display is surrounded by
  14. // control knobs.  Running the program with an argument will
  15. // make it make a seperate GL window from the controls window.  This
  16. // simulates a (older?) style program where the graphics display is
  17. // a different window than the controls.
  18. //
  19. // This program reports how many times it redraws the window to
  20. // stdout, so you can see how much time it is wasting.  It appears
  21. // to be impossible to prevent X from sending redundant resize
  22. // events, so there are extra redraws.  But the way I have the
  23. // code arranged here seems to be keeping that to a minimu.
  24. //
  25. // Apparently unavoidable bugs:
  26. //
  27. // Turning the border on causes an unnecessary redraw.
  28. //
  29. // Turning off full screen when the border is on causes an unnecessary
  30. // resize and redraw when the program turns the border on.
  31. //
  32. // If it is a seperate window, turning double buffering on and off
  33. // will cause the window to raise, deiconize, and possibly move.  You
  34. // can avoid this by making the Fl_Gl_Window a child of a normal
  35. // window.
  36. //
  37. // Copyright 1998-1999 by Bill Spitzak and others.
  38. //
  39. // This library is free software; you can redistribute it and/or
  40. // modify it under the terms of the GNU Library General Public
  41. // License as published by the Free Software Foundation; either
  42. // version 2 of the License, or (at your option) any later version.
  43. //
  44. // This library is distributed in the hope that it will be useful,
  45. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  46. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  47. // Library General Public License for more details.
  48. //
  49. // You should have received a copy of the GNU Library General Public
  50. // License along with this library; if not, write to the Free Software
  51. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  52. // USA.
  53. //
  54. // Please report all bugs and problems to "fltk-bugs@easysw.com".
  55. //
  56.  
  57. #include <config.h>
  58. #include <FL/Fl.H>
  59. #include <FL/Fl_Single_Window.H>
  60. #include <FL/Fl_Hor_Slider.H>
  61. #include <FL/Fl_Toggle_Light_Button.H>
  62. #include <FL/math.h>
  63. #include <stdio.h>
  64.  
  65. #if HAVE_GL
  66. #include <FL/gl.h>
  67. #include <FL/Fl_Gl_Window.H>
  68.  
  69. class shape_window : public Fl_Gl_Window {
  70.   void draw();
  71. public:
  72.   int sides;
  73.   shape_window(int x,int y,int w,int h,const char *l=0);
  74. };
  75.  
  76. shape_window::shape_window(int x,int y,int w,int h,const char *l) :
  77. Fl_Gl_Window(x,y,w,h,l) {
  78.   sides = 3;
  79. }
  80.  
  81. void shape_window::draw() {
  82.   printf("drawing size %d %d\n",w(),h());
  83.   if (!valid()) {
  84.     valid(1);
  85. //  printf("init\n");
  86.     glLoadIdentity();
  87.     glViewport(0,0,w(),h());
  88.   }
  89.   glClear(GL_COLOR_BUFFER_BIT);
  90.   glColor3f(.5,.6,.7);
  91.   glBegin(GL_POLYGON);
  92.   for (int i=0; i<sides; i++) {
  93.     double ang = i*2*M_PI/sides;
  94.     glVertex3f(cos(ang),sin(ang),0);
  95.   }
  96.   glEnd();
  97. }
  98.  
  99. #else
  100.  
  101. #include <FL/fl_draw.H>
  102.  
  103. class shape_window : public Fl_Window {
  104.   void draw();
  105. public:
  106.   int sides;
  107.   shape_window(int x,int y,int w,int h,const char *l=0);
  108. };
  109.  
  110. shape_window::shape_window(int x,int y,int w,int h,const char *l) :
  111. Fl_Window(x,y,w,h,l) {
  112.   sides = 3;
  113. }
  114.  
  115. void shape_window::draw() {
  116.   fl_color(0);
  117.   fl_rectf(0,0,w(),h());
  118.   fl_font(0,20);
  119.   fl_color(7);
  120.   fl_draw("This requires GL",0,0,w(),h(),FL_ALIGN_CENTER);
  121. }
  122.  
  123. #endif
  124.  
  125. void sides_cb(Fl_Widget *o, void *p) {
  126.   shape_window *sw = (shape_window *)p;
  127.   sw->sides = int(((Fl_Slider *)o)->value());
  128.   sw->redraw();
  129. }
  130.  
  131. #if HAVE_GL
  132. void double_cb(Fl_Widget *o, void *p) {
  133.   shape_window *sw = (shape_window *)p;
  134.   int d = ((Fl_Button *)o)->value();
  135.   sw->mode(d ? Fl_Mode(FL_DOUBLE|FL_RGB) : FL_RGB);
  136. }
  137. #else
  138. void double_cb(Fl_Widget *, void *) {}
  139. #endif
  140.  
  141. void border_cb(Fl_Widget *o, void *p) {
  142.   Fl_Window *w = (Fl_Window *)p;
  143.   int d = ((Fl_Button *)o)->value();
  144.   w->border(d);
  145. }
  146.  
  147. int px,py,pw,ph;
  148. Fl_Button *border_button;
  149. void fullscreen_cb(Fl_Widget *o, void *p) {
  150.   Fl_Window *w = (Fl_Window *)p;
  151.   int d = ((Fl_Button *)o)->value();
  152.   if (d) {
  153.     px = w->x();
  154.     py = w->y();
  155.     pw = w->w();
  156.     ph = w->h();
  157.     w->fullscreen();
  158.   } else {
  159.     w->fullscreen_off(px,py,pw,ph);
  160.   }
  161. }
  162.  
  163. #include <stdlib.h>
  164.  
  165. void exit_cb(Fl_Widget *, void *) {
  166.   exit(0);
  167. }
  168.  
  169. #define NUMB 5
  170.  
  171. int twowindow = 0;
  172. int initfull = 0;
  173. int arg(int, char **argv, int &i) {
  174.   if (argv[i][1] == '2') {twowindow = 1; i++; return 1;}
  175.   if (argv[i][1] == 'f') {initfull = 1; i++; return 1;}
  176.   return 0;
  177. }
  178.  
  179. int main(int argc, char **argv) {
  180.  
  181.   int i=0;
  182.   if (Fl::args(argc,argv,i,arg) < argc)
  183.     Fl::fatal("Options are:\n -2 = 2 windows\n -f = startup fullscreen\n%s",Fl::help);
  184.  
  185.   Fl_Single_Window window(300,300+30*NUMB); window.end();
  186.  
  187.   shape_window sw(10,10,window.w()-20,window.h()-30*NUMB-20);
  188. #if HAVE_GL
  189.   sw.mode(FL_RGB);
  190. #endif
  191.  
  192.   Fl_Window *w;
  193.   if (twowindow) {    // make it's own window
  194.     sw.resizable(&sw);
  195.     w = &sw;
  196.     window.set_modal();    // makes controls stay on top when fullscreen pushed
  197.     argc--;
  198.     sw.show();
  199.   } else {        // otherwise make a subwindow
  200.     window.add(sw);
  201.     window.resizable(&sw);
  202.     w = &window;
  203.   }
  204.  
  205.   window.begin();
  206.  
  207.   int y = window.h()-30*NUMB-5;
  208.   Fl_Hor_Slider slider(50,y,window.w()-60,30,"Sides:");
  209.   slider.align(FL_ALIGN_LEFT);
  210.   slider.callback(sides_cb,&sw);
  211.   slider.value(sw.sides);
  212.   slider.step(1);
  213.   slider.bounds(3,40);
  214.   y+=30;
  215.  
  216.   Fl_Toggle_Light_Button b1(50,y,window.w()-60,30,"Double Buffered");
  217.   b1.callback(double_cb,&sw);
  218.   y+=30;
  219.  
  220.   Fl_Toggle_Light_Button b2(50,y,window.w()-60,30,"Border");
  221.   b2.callback(border_cb,w);
  222.   b2.set();
  223.   border_button = &b2;
  224.   y+=30;
  225.  
  226.   Fl_Toggle_Light_Button b3(50,y,window.w()-60,30,"FullScreen");
  227.   b3.callback(fullscreen_cb,w);
  228.   y+=30;
  229.  
  230.   Fl_Button eb(50,y,window.w()-60,30,"Exit");
  231.   eb.callback(exit_cb);
  232.   y+=30;
  233.  
  234.   if (initfull) {b3.set(); b3.do_callback();}
  235.  
  236.   window.end();
  237.   window.show(argc,argv);
  238.  
  239.   return Fl::run();
  240. }
  241.  
  242. //
  243. // End of "$Id: fullscreen.cxx,v 1.4 1999/01/07 19:17:54 mike Exp $".
  244. //
  245.